# generate game and player templates to save to disk
# note - to run, the game and agent classes must be defined

updateGameTemplates <- function(saveDir) {
	setwd(saveDir)
	agentTemplate <- list()
	gameTemplate <- list()
	
	agentTemplate$bmAlpha01 <-list(aspiration=1.5, 
                                 prevAspWgt=0.7,
                                 refWgt=0.15,
                                 refGrp=as.integer(2), 
                                 extraParms=list(alphaFunction=function(x){return(0.1)},
                                                 propInertiaFct=function(x){return(0.01)},
                                                 aspInertiaProb=0.01), 
                                 trembleProb=0.01)
  
	agentTemplate$bmAlpha01asocial <-list(aspiration=1.5, 
                                        prevAspWgt=0.7, 
                                        extraParms=list(alphaFunction=function(x){return(0.1)},
                                                        propInertiaFct=function(x){return(0.01)},
                                                        aspInertiaProb=0.01), 
                                        trembleProb=0.01)
  
	agentTemplate$bmMagSens <- list(aspiration=1.5, 
                                  prevAspWgt=0.7,
                                  refWgt=0.15,
                                  refGrp=as.integer(2), 
                                  extraParms=list(alphaFunction=function(x){return(1 - exp(-x^2))},
                                                  propInertiaFct=function(x){return(0.01)},
                                                  aspInertiaProb=0.01), 
                                  trembleProb=0.01)
  
	agentTemplate$bmMagSensAsocial <- list(aspiration=1.5, 
                                         prevAspWgt=0.7, 
                                         extraParms=list(alphaFunction=function(x){return(1 - exp(-x^2))},
                                                         propInertiaFct=function(x){return(0.01)},
                                                         aspInertiaProb=0.01), 
                                         trembleProb=0.01)
  
	agentTemplate$bmPropInertiaSens <-list(aspiration=1.5, 
	                               prevAspWgt=0.7,
	                               refWgt=0.15,
	                               refGrp=as.integer(2), 
	                               extraParms=list(alphaFunction=function(x){return(0.1)},
	                                               propInertiaFct=function(x){return(exp(-x^2))},
	                                               aspInertiaProb=0.01), 
	                               trembleProb=0.01)
	
	agentTemplate$bmPropInertiaSensAsocial <-list(aspiration=1.5, 
	                                      prevAspWgt=0.7, 
	                                      extraParms=list(alphaFunction=function(x){return(0.1)},
	                                                      propInertiaFct=function(x){return(exp(-x^2))},
	                                                      aspInertiaProb=0.01), 
	                                      trembleProb=0.01)
	
	payoffMat1 <- matrix(data=c(8.01,1,0,2), nrow=2,ncol=2)
	payoffMat2 <- matrix(data=c(8,0,1,2.01), nrow=2, ncol=2)
	payoffList <- list(p1=payoffMat1, p2=payoffMat2)
	gameTemplate$BDT.V1 <- payoffList
	
	payoffMat1 <- matrix(data=c(100.01,1,0,2), nrow=2,ncol=2)
	payoffMat2 <- matrix(data=c(100,0,1,2.01), nrow=2, ncol=2)
	payoffList <- list(p1=payoffMat1, p2=payoffMat2)
	gameTemplate$BDT.V1a <- payoffList
	
	payoffMat1 <- matrix(data=c(8.01,1,0,2), nrow=2,ncol=2)
	payoffMat2 <- matrix(data=c(8,0,1,2), nrow=2, ncol=2)
	payoffList <- list(p1=payoffMat1, p2=payoffMat2)
	gameTemplate$BDT.V2 <- payoffList

	payoffMat1 <- matrix(data=c(8,1,0,2), nrow=2,ncol=2)
	payoffMat2 <- matrix(data=c(8,0,1,2), nrow=2, ncol=2)
	payoffList <- list(p1=payoffMat1, p2=payoffMat2)
	gameTemplate$BDT.V2a <- payoffList
	
	payoffMat1 <- matrix(data=c(1,3,2,0), nrow=2,ncol=2)
	payoffMat2 <- matrix(data=c(1,2,3,0), nrow=2, ncol=2)
	payoffList <- list(p1=payoffMat1, p2=payoffMat2)
	gameTemplate$BDT.V3 <- payoffList
	
	payoffMat1 <- matrix(data=c(2,1,1,0), nrow=2,ncol=2)
	payoffMat2 <- matrix(data=c(2,1,1,0), nrow=2, ncol=2)
	payoffList <- list(p1=payoffMat1, p2=payoffMat2)
	gameTemplate$BDT.V4 <- payoffList
	
	payoffMat1 <- matrix(data=c(2,1,2,0), nrow=2,ncol=2)
	payoffMat2 <- matrix(data=c(2,2,1,0), nrow=2, ncol=2)
	payoffList <- list(p1=payoffMat1, p2=payoffMat2)
	gameTemplate$BDT.V5 <- payoffList

	payoffMat1 <- matrix(data=c(3,1,0,2), nrow=2,ncol=2)
	payoffMat2 <- matrix(data=c(3,0,1,2), nrow=2, ncol=2)
	payoffList <- list(p1=payoffMat1, p2=payoffMat2)
	gameTemplate$BDT.V6a <- payoffList

	payoffMat1 <- matrix(data=c(3.001,1,0,2.001), nrow=2,ncol=2)
	payoffMat2 <- matrix(data=c(3,0,1,2), nrow=2, ncol=2)
	payoffList <- list(p1=payoffMat1, p2=payoffMat2)
	gameTemplate$BDT.V6b <- payoffList

	payoffMat1 <- matrix(data=c(3.01,1,0,2.01), nrow=2,ncol=2)
	payoffMat2 <- matrix(data=c(3,0,1,2), nrow=2, ncol=2)
	payoffList <- list(p1=payoffMat1, p2=payoffMat2)
	gameTemplate$BDT.V6c <- payoffList

	payoffMat1 <- matrix(data=c(1,3,2,0), nrow=2,ncol=2)
	payoffMat2 <- matrix(data=c(1,2,3,0), nrow=2, ncol=2)
	payoffList <- list(p1=payoffMat1, p2=payoffMat2)
	gameTemplate$BDT.V7a <- payoffList

	payoffMat1 <- matrix(data=c(1.001,3,2,0.001), nrow=2,ncol=2)
	payoffMat2 <- matrix(data=c(1,2,3,0), nrow=2, ncol=2)
	payoffList <- list(p1=payoffMat1, p2=payoffMat2)
	gameTemplate$BDT.V7b <- payoffList

	payoffMat1 <- matrix(data=c(1.01,3,2,0.01), nrow=2,ncol=2)
	payoffMat2 <- matrix(data=c(1,2,3,0), nrow=2, ncol=2)
	payoffList <- list(p1=payoffMat1, p2=payoffMat2)
	gameTemplate$BDT.V7c <- payoffList
  
	payoffMat1 <- matrix(data=c(0.01,-100,-100,10), nrow=2,ncol=2)
	payoffMat2 <- matrix(data=c(-0.01,100,100,-10), nrow=2, ncol=2)
	payoffList <- list(p1=payoffMat1, p2=payoffMat2)
	gameTemplate$BDT.V8a <- payoffList
	
	payoffMat1 <- matrix(data=c(0.01,-3,-3,2), nrow=2,ncol=2)
	payoffMat2 <- matrix(data=c(-0.01,3,3,-2), nrow=2, ncol=2)
	payoffList <- list(p1=payoffMat1, p2=payoffMat2)
	gameTemplate$BDT.V8b <- payoffList
	
	payoffMat1 <- matrix(data=c(1,-3,-3,2), nrow=2,ncol=2)
	payoffMat2 <- matrix(data=c(-1,3,3,-2), nrow=2, ncol=2)
	payoffList <- list(p1=payoffMat1, p2=payoffMat2)
	gameTemplate$BDT.V8c <- payoffList
	
	save(agentTemplate, gameTemplate, file="gameTemplates-BDT.Rimage")
}

createAgentProto <- function(type, id, nActions, abar=NULL, paramsList=NULL, refGrp=NULL) {
	
	if (type == "Agent") {
		agent <- new("Agent", id=as.integer(id), nActions=nActions)
	} else if (type == "abarAgent") {
		if (abar == "satisficing") {
			a <- paramsList$aspiration
			pw <- paramsList$prevAspWgt
			ep <- paramsList$extraParms
			tp <- paramsList$trembleProb
		
			agent <- new("abarAgent", id=as.integer(id), trembleProb=tp, propUpdateFunction=propUpdateSatisfice, nActions=nActions, aspiration=a,prevAspWgt=pw,extraParms=ep)
		} else if (abar == "bm") {
			a <- paramsList$aspiration
			pw <- paramsList$prevAspWgt
			ep <- paramsList$extraParms
			tp <- paramsList$trembleProb
			
			agent <- new("abarAgent", id=as.integer(id), trembleProb=tp,propUpdateFunction=propUpdateBM, nActions=nActions, aspiration=a,prevAspWgt=pw,extraParms=ep)
		}	
	} else if (type == "socialAgent") {
		if (abar == "satisficing") {
			a <- paramsList$aspiration
			pw <- paramsList$prevAspWgt
			ep <- paramsList$extraParms
			rw <- paramsList$refWgt
			tp <- paramsList$trembleProb
		
			agent <- new("socialAgent", id=as.integer(id), trembleProb=tp,propUpdateFunction=propUpdateSatisfice, nActions=nActions, aspiration=a,prevAspWgt=pw,refWgt=rw,refGrp=as.integer(refGrp),extraParms=ep)
		} else if (abar == "bm") {
			a <- paramsList$aspiration
			pw <- paramsList$prevAspWgt
			ep <- paramsList$extraParms
			rw <- paramsList$refWgt
			tp <- paramsList$trembleProb
		
			agent <- new("socialAgent", id=as.integer(id), trembleProb=tp,propUpdateFunction=propUpdateBM, nActions=nActions, aspiration=a,prevAspWgt=pw,refWgt=rw,refGrp=as.integer(refGrp),extraParms=ep)
		}
	}
	return(agent)
}

